#include <gtest/gtest.h>
#include "inverted_index.h"

TEST(InvertedIndex, encode)
{
    PostingList postingList("abc");

    postingList.InsertPosing(1, 10);
    postingList.InsertPosing(1, 20);
    postingList.InsertPosing(2, 30);

    ASSERT_EQ(postingList.GetToken(), "abc");
    ASSERT_EQ(postingList.GetOccur(), 3);

    unsigned char *buf;
    int size;
    postingList.Encode(&buf, size);

    int *data = reinterpret_cast<int *>(buf);
    ASSERT_EQ(data[0], 4 + 8 + 8 + 8 + 4);
    ASSERT_EQ(data[1], 1);
    ASSERT_EQ(data[2], 2);
    ASSERT_EQ(data[3], 10);
    ASSERT_EQ(data[4], 20);
    ASSERT_EQ(data[5], 2);
    ASSERT_EQ(data[6], 1);
    ASSERT_EQ(data[7], 30);

    delete [] buf;
}

TEST(InvertedIndex, decode)
{
    PostingList postingList("abc");

    int data[8] = {32, 1, 2, 10, 20, 2, 1, 30};

    postingList.Decode(reinterpret_cast<unsigned char *>(data), sizeof(data));

    ASSERT_EQ(postingList.GetToken(), "abc");
    ASSERT_EQ(postingList.GetOccur(), 3);
}